﻿using AutoMapper;
using ERP.Inventory.Api.Read.Application.Command.库存管理;
using ERP.Inventory.Api.Read.DTO;
using ERP.Inventory.Domain.库存管理;
using ERP.Inventory.Errorcode;
using ERP.Inventory.Infastructure.Interface;
using MediatR;

namespace ERP.Inventory.Api.Read.Application.Handler.库存管理
{
    public class DetailsHandler : IRequestHandler<DetailsCommand, ApiResult<CheckDTO>>
    {
        private readonly IBaseRepository<CheckModel> CheckRepo;
        private readonly IBaseRepository<StoreModel> storeRepo;

        public DetailsHandler(IBaseRepository<CheckModel> CheckRepo, IBaseRepository<StoreModel> storeRepo)
        {
            this.CheckRepo = CheckRepo;
            this.storeRepo = storeRepo;
        }

        public Task<ApiResult<CheckDTO>> Handle(DetailsCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<CheckDTO>();
            var l = from a in CheckRepo.GetAll()
                    join b in storeRepo.GetAll()
                    on a.StoreId equals b.Id
                    where a.IsDel == false
                    select new CheckDTO
                    {
                        Id = a.Id,
                        InvenCode = a.InvenCode,
                        InvenState = a.InvenState,
                        Mid = a.Mid,
                        InvenType = a.InvenType,
                        StoreId = a.StoreId,
                        StoreName = b.StoreName,
                        InvenRange = a.InvenRange,
                        InvenStartTime = a.InvenStartTime,
                        InvenEndTime = a.InvenEndTime,
                        Note = a.Note,
                        AttnId = a.AttnId,
                        AttnName = a.AttnName,
                        CreateUserTime = a.CreateUserTime,
                    };
            var trac = l.FirstOrDefault(x => x.InvenCode == request.InvenCode);
            res.Message = "详情显示成功";
            res.Code = ApiEnum.Ok;
            res.Data = trac;
            return Task.FromResult(res);
        }
    }
}
